package ModelLayer;
import java.util.ArrayList;

/**
 * 
 * @Author (Alexander Hvidberg, Dennis Kirkebak & Thomas Kulmbach)
 * @Team (Group 1)
 * @Version (08/11/13)
 * 
 */
public class RentalContainer
{
    // instance variables
    private static RentalContainer instance;
    private ArrayList<Rental> rentals;
    private int lastId;

    /**
     * Constructor for objects of class RentalContainer
     */
    private RentalContainer()
    {
        // initialise instance variables
        rentals = new ArrayList<Rental>();
        lastId = 1;
        
    }
    
    /**
     * Singleton
     */
    public static RentalContainer getInstance()
    {
        if(instance == null) {
            instance = new RentalContainer();
        }
        return instance;
    }
    
    /**
     * Create a new Rental and add it to this Arraylist
     * 
     * @param String date - the date of the rental
     * @param int period - the period of the rental
     * @param Person p - the person of the rental
     * @param DVD d -the dvd of the rental
     */
    public void createRental(String date, int period, Person p, DVD d)
    {
        if(d.getArticlesSize()<1){
            System.out.println("There is no more available copies of that DVD");
        }
        else{
            rentals.add(new Rental(lastId, date, period, p, d));
            lastId++;
        }
    }
    
    /**
     * Find a rental in the collection
     * 
     * @param int id - the id
     * 
     * @return Rental r - the rental
     */
    public Rental findRental(int id)
    {
        int index = 0;
        Rental r = null;
        boolean found = false;
        while(index<rentals.size() && !found) {
            if(id==rentals.get(index).getId()) {
                r = rentals.get(index);
                found = true;
            }
            else {
                index++;
            }
        }
        return r;
    }
    
    /**
     * Update the information about a rental with a given id
     * 
     * @param int id - the rental id
     * @param String date - the rental date 
     * @param int period -the rental period
     * @param Person p - the rental person
     * @param DVD d - the rental dvd
     */
    public void updateRental(int id, String date, int period, Person p, DVD d)
    {
        Rental r = findRental(id);
        r.updateRental(date, period, p, d);
    }
    
    /**
     * Delete a Rental with a given id.
     * 
     * @param int id - the rental id
     */
    public void deleteRental(int id)
    {
        Rental r = findRental(id);
        int an = r.getArticleNumber();
        r.unrent(an);
        rentals.remove(r);
        
        
    }
    
    /**
     * Show a list of all Rentals
     * 
     * @return ArrayList rentals - a list of rentals
     */
    public ArrayList getRentalList()
    {
        return rentals;
    }
    
    /**
     * Print details of the rentals.
     */
    public void printRentalList()
    {
        for(Rental rental : rentals) {
            rental.printDetails();
        }
    }
}
